/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package duplicatefilemanager;

/**
 *
 * @author Pastor Albano Jr
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class FindDuplicates {
    private static MessageDigest md;
    static {
        try {
            md = MessageDigest.getInstance("SHA-512");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("cannot initialize SHA-512 hash function", e);
        }
    }

    public static void find(Map<String, List<String>> lists, File directory) {
        for (File child : directory.listFiles()) {
            if (child.isDirectory()) {
                find(lists, child);
            } else {
                try {
                    FileInputStream fin = new FileInputStream(child);
                    byte data[] = new byte[(int) child.length()];
                    fin.read(data);
                    fin.close();
                    String hash = new BigInteger(1, md.digest(data)).toString(16);
                    List<String> list = lists.get(hash);
                    if (list == null) {
                        list = new LinkedList<String>();
                        lists.put(hash, list);
                    }
                    list.add(child.getAbsolutePath());
                } catch (IOException e) {
                    throw new RuntimeException("cannot read file " + child.getAbsolutePath(), e);
                }
            }
        }
    }

    public static void main(String[] args) {
    /*    if (args.length < 1) {
            System.out.println("Please supply a path to directory to find duplicate files in.");
            return;
        }	*/
        // "C:/Users/Bryan Joseph/Desktop/FRANZ"
    //    printDuplicates(new File("C:/Users/Bryan Joseph/Desktop/FRANZ"));
    }
    
    public static void printDuplicates(String path, DefaultListModel model, JLabel label1, JLabel label2) {
        File dir = new File(path);
        if (!dir.isDirectory()) {
            System.out.println("Supplied directory does not exist.");
            return;
        }
        Map<String, List<String>> lists = new HashMap<String, List<String>>();
        FindDuplicates.find(lists, dir);
        int value1 = 0;
        int value2 = 0;
        for (List<String> list : lists.values()) {
            if (list.size() > 1) {
                for (String file : list) {
                    value1++;
                    value2++;
                    model.addElement(file);
                }
            }
            value1++;
        }
        if (model.size()>1) {
        	value1--;
        }
        int result1 = value1 + Integer.parseInt(label1.getText());
        int result2 = value2 + Integer.parseInt(label2.getText());
        label1.setText(Integer.toString(result1));
        label2.setText(Integer.toString(result2));
    }  

}
